Repository navigation
Fix the pool connection acquisition timeout - #1807
tsegismont wants to merge 1 commit into
Conversation
A pooled query that timed out waiting for a connection kept its place in the pool wait queue, so later requests were rejected with a `ConnectionPoolTooBusyException` although nothing was waiting, and the queue metric stayed inflated. `Pool#getConnection` could also hang forever: when the timer fired as the pool emitted a lease, `Vertx#cancelTimer` and `ConnectionPool#cancel` both reported a loss for the same request and neither completed it, leaking the queue metric as well. Both paths now share a `PoolRequest` whose lifecycle is arbitrated by a single atomically updated state, so exactly one of the timer and the pool completes the request. The timer is set from the waiter listener callbacks, where the waiter is known, so a timed out request always leaves the wait queue before it fails. `Pool#getConnection` now reports a timeout with `Timeout waiting for connection` instead of `Timeout`. A pooled query no longer hops back to the context of the caller to schedule its command, it reports the command on that context explicitly instead. As a consequence an acquisition failure is no longer dispatched on the context of the caller, which `CommandScheduler#schedule` now documents. A pooled query no longer applies the `beforeRecycle` hook when the `afterAcquire` hook fails, like `Pool#getConnection` already did. Assisted-by: Anthropic Claude (claude-opus-5) Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
| private final long timeout; | ||
| private final Object metric; | ||
|
|
||
| private volatile long state = NO_TIMER; |
There was a problem hiding this comment.
can we simply use an AtomicLong here ? atomic field updates are useful under intensive usage, here we care mostly about visibility and correctness
There was a problem hiding this comment.
To my knowledge, the field updater is useful when many instances are created, which is the case here: one PoolRequest per pool query or connection acquisition. But I have no strong opinion, if you prefer AtomicLong I will change it.
|
I think overrall we can improve the design : cancelTimer returns a boolean indicating whether the timer will execute, when completing the section we should use this instead of the state to decide whether we should continue or the timer ran effectively and no action should be done |
Agreed, cancelTimer is the right arbiter for this:
However I don't see how to remove the state field completely. It would still be needed to store the timer id, and to prevent arming a timer for a request completed already, or two timers for the same request:
If the pool executed the listener callbacks ordered with the other operations (inside the combiner, or at least before the delivery to the same waiter), a plain volatile timer id would be enough, without the CAS and the completed state. Perhaps something to discuss for vert.x core. An API to cancel a waiter without holding the Did you mean the state field can be removed completely? If you see how, I am interested. |
Motivation: Adopt the new Vert.x connection pool implementation (eclipse-vertx/vert.x#6587) that will provide more features, one of them is getting away from a timer per acquisition since the pool owns the acquisition lifecycle (#1807, #1342). Changes: The pool checks staleness, closes the connections it evicts and times out acquisitions with a TimeoutException. As a side effect this fixes the bug addressed by #1807: a timed out query no longer keeps its slot in the wait queue, asserted by a test ported from that PR. A periodic refresh, configured by the new refresh period option (100 ms default), drives all the maintenance tasks; the pool cleaner period is deprecated. The queue metric only accounts acquisitions that effectively wait. Assisted-by: Anthropic Claude (claude-fable-5) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Signed-off-by: Julien Viet <julien@julienviet.com>
Motivation: Adopt the new Vert.x connection pool implementation (eclipse-vertx/vert.x#6587) that will provide more features, one of them is getting away from a timer per acquisition since the pool owns the acquisition lifecycle (#1807, #1342). Changes: The pool checks staleness with the connection checker configured in the builder, closes the connections it evicts and times out acquisitions with a TimeoutException. As a side effect this fixes the bug addressed by #1807: a timed out query no longer keeps its slot in the wait queue, asserted by a test ported from that PR. A mandatory periodic refresh, configured by the new refresh period option (a Duration of at least one millisecond, 100 ms by default), drives all the maintenance tasks; the pool cleaner period is deprecated. The queue metric only accounts acquisitions that effectively wait. Assisted-by: Anthropic Claude (claude-fable-5) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Signed-off-by: Julien Viet <julien@julienviet.com>
|
Superseded by #1820 |
Motivation: Adopt the new Vert.x connection pool implementation (eclipse-vertx/vert.x#6587) that will provide more features, one of them is getting away from a timer per acquisition since the pool owns the acquisition lifecycle (#1807, #1342). Changes: The pool checks staleness with the connection checker configured in the builder, closes the connections it evicts and times out acquisitions with a TimeoutException. As a side effect this fixes the bug addressed by #1807: a timed out query no longer keeps its slot in the wait queue, asserted by a test ported from that PR. A mandatory periodic refresh, configured by the new refresh period option (a Duration of at least one millisecond, 100 ms by default), drives all the maintenance tasks; the pool cleaner period is deprecated. The queue metric only accounts acquisitions that effectively wait. Assisted-by: Anthropic Claude (claude-fable-5) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Signed-off-by: Julien Viet <julien@julienviet.com>
A pooled query that timed out waiting for a connection kept its place in the pool wait queue, so later requests were rejected with a
ConnectionPoolTooBusyExceptionalthough nothing was waiting, and the queue metric stayed inflated.Pool#getConnectioncould also hang forever: when the timer fired as the pool emitted a lease,Vertx#cancelTimerandConnectionPool#cancelboth reported a loss for the same request and neither completed it, leaking the queue metric as well.Both paths now share a
PoolRequestwhose lifecycle is arbitrated by a single atomically updated state, so exactly one of the timer and the pool completes the request. The timer is set from the waiter listener callbacks, where the waiter is known, so a timed out request always leaves the wait queue before it fails.Pool#getConnectionnow reports a timeout withTimeout waiting for connectioninstead ofTimeout.A pooled query no longer hops back to the context of the caller to schedule its command, it reports the command on that context explicitly instead. As a consequence an acquisition failure is no longer dispatched on the context of the caller, which
CommandScheduler#schedulenow documents.A pooled query no longer applies the
beforeRecyclehook when theafterAcquirehook fails, likePool#getConnectionalready did.Assisted-by: Anthropic Claude (claude-opus-5)